<template>
  <!-- 概况信息 下 的 回款详情 组件 -->
  <div class="ReceivableDetail">
    <zm-form :model="searchForm" ref="searchFormRef" inline size="small">
      <zm-form-item label prop="orderNum">
        <zm-input
          v-model="searchForm.returnedNo"
          placeholder="回款单号、销售单号"
        ></zm-input>
      </zm-form-item>
      <zm-form-item label prop="customerName">
        <zm-input
          v-model="searchForm.customerName"
          placeholder="关联客户"
        ></zm-input>
      </zm-form-item>
      <zm-form-item>
        <zm-button @click="onMySearch" t="search">查询</zm-button>
        <zm-button @click="onReset" t="reset">重置</zm-button>
      </zm-form-item>
      <el-button
        @click="handelFull"
        v-if="infoObj.status === '0'"
        class="ReceivableDetail-button__fillBtn"
        >
一键填入
</el-button>
    </zm-form>

    <zm-table-data
      class="ReceivableDetail-table__myTableData"
      stripe
      height="500"
      :data="tableParams.data"
      :columns="receivableDetailsList"
      :fetch="loadList"
      :table-params="tableParams"
      :show-page="false"
    />
    <detail-container
      v-if="extendDetailParams.visible"
      v-model="extendDetailParams.visible"
      :id="extendDetailParams.id"
      :business-id="extendDetailParams.id"
      :is-show-btn="false"
      :type="extendDetailParams.type"
    />
  </div>
</template>

<script>
import {receivableDetailsList} from './const.js'
import {getPageList} from '@/api/src/returnedMoney'
import tableMixin from '@/mixins/zmTableMixin'
import DetailContainer from '@/components/DetailContainer'
export default {
  name: 'ReceivableDetail',
  components: {DetailContainer},
  mixins: [tableMixin],
  props: {
    month: {
      type: String,
      required: true
    },
    infoObj: {
      type: Object,
      required: true
    }
  },
  data () {
    return {
      searchForm: {
        returnedNo: '', // 回款单号或销售单号
        customerName: '' // 关联客户
      },
      extendDetailParams: {
        visible: false,
        id: '',
        type: '',
        obj: {}
      },
      verifyDate: []
    }
  },
  computed: {
    receivableDetailsList () {
      return receivableDetailsList(this) // const.js 需要用到 infoObj.status
    },
    allverifyPrice () {
      let total = 0
      this.verifyDate.forEach(item => {
        let verifyPrice = parseFloat(item.verifyPrice)
        verifyPrice = (isNaN(verifyPrice) ? 0 : verifyPrice)
        total = (this.accAdd(total, verifyPrice)).toFixed(2)
      })
      return total
    }
  },
  watch: {
    month () {
      this.onSearch()
    },
    allverifyPrice () {
      this.$emit('getVerifyDate', this.verifyDate, this.allverifyPrice)
    }
  },
  created () {

  },
  methods: {
    async loadList () {
      let _res = await getPageList({...this.searchForm, month: this.month, listType: '2', salesmanId: this.infoObj.salemanId})
      this.verifyDate = _res.list
      if (this.infoObj.status === '0') {
        _res.list.forEach(item => {
          item.verifyPrice = undefined
        })
      }
      this.$emit('getVerifyDate', this.verifyDate, this.allverifyPrice)
      this.initialTableData(_res.list)
    },
    initialTableData (dataList, totalCount) {
      const tableParams = this.tableParams
      tableParams.total = totalCount || 0
      tableParams.data = dataList
    },
    handelFull () {
      this.tableParams.data.forEach(item => {
        item.verifyPrice = item.returnedPrice
      })
    },
    onReset () {
      if (this.$refs.searchFormRef) {
        this.$refs.searchFormRef.resetFields()
        this.searchForm.returnedNo = ''
        this.searchForm.customerName = ''
        this.onSearch()
      }
    },
    onExtendShow (id, type) {
      this.extendDetailParams = {
        visible: true,
        id: id,
        businessId: id,
        type: type
      }
    },
    onMySearch () {
      let newDate = this.verifyDate.filter((item) => {
        return (item.returnedNo.includes(this.searchForm.returnedNo) || item.saleOrderNo.includes(this.searchForm.returnedNo)) && item.customerName.includes(this.searchForm.customerName)
      })
      this.initialTableData(newDate)
    },
    // 小数计算加法
    accAdd (arg1, arg2) {
      var r1, r2, m
      try {
        r1 = arg1.toString().split('.')[1].length
      } catch (e) {
        r1 = 0
      }
      try {
        r2 = arg2.toString().split('.')[1].length
      } catch (e) {
        r2 = 0
      }
      // 这里Math.max(r1, r2)取最长的小数位
      // Math.ceil(length / 3) * 3) 按照位数计算乘以的倍数.比如 2 => 1000, 4 => 1000000
      m = Math.pow(10, Math.ceil(Math.max(r1, r2) / 3) * 3)
      return (arg1 * m + arg2 * m) / m
    }

  }
}
</script>

<style lang="scss" scoped>
.ReceivableDetail {
  .ReceivableDetail-button__fillBtn {
    float: right;
    color: #48a2ff;
    border: 1px solid #48a2ff;
  }
}
</style>
